package com.kaikai.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaikai.commonutils.R;
import com.kaikai.edu.entity.EduTeacher;
import com.kaikai.edu.entity.vo.TeacherQuery;
import com.kaikai.edu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author yujiankai
 * @since 2021-05-02
 */
//Swagger提示
@Api("讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    EduTeacherService teacherService;

    @ApiOperation(value = "查询所有讲师")
    @GetMapping("findAll")
    public R findAll() {
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "根据id逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", required = true, value = "讲师ID") @PathVariable String id) {
        return teacherService.removeTeacherById(id);
    }

    @ApiOperation(value = "分页查询讲师")
    @PostMapping("pageTeacher/{current}/{size}")
    public R pageListTeacher(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable Long current,
                             @ApiParam(name = "size", value = "每页记录数", required = true) @PathVariable Long size) {
        Page<EduTeacher> page = new Page<>(current, size);
        teacherService.page(page, null);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();


        return R.ok().data("total", total).data("rows", records);

    }

    /**
     * @param current
     * @param size
     * @param teacherQuery
     * @return
     */
    @ApiOperation(value = "多条件查询讲师")
    @PostMapping("pageTeacherCondition/{current}/{size}")
    public R pageTeacherCondition(@ApiParam(value = "当前页") @PathVariable Long current,
                                  @ApiParam(value = "每页记录数") @PathVariable Long size,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> teacherPage = new Page<>(current, size);
        //构建条件wrapper
        //多条件查询
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (StringUtils.hasText(name)) {
            wrapper.like("name", name);

        }
        if (!"".equals(level)&&level!=null) {

            wrapper.eq("level", level);

        }
        if (StringUtils.hasText(begin)) {
            wrapper.ge("gmt_create", begin);

        }
        if (StringUtils.hasText(end)) {
            System.out.println(end);

            wrapper.le("gmt_modified", end);
        }
        //添加时间排序
        wrapper.orderByDesc("gmt_modified");


        teacherService.page(teacherPage, wrapper);

        List<EduTeacher> records = teacherPage.getRecords();

        long total = teacherPage.getTotal();
        return R.ok().data("records", records).data("total", total);
    }
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        }
        return R.error();
    }
    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("{id}")
    public R findTeacherById(@ApiParam(name = "id", required = true, value = "讲师id") @PathVariable String id) {
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
    }
    @ApiOperation(value = "更新讲师信息")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean b = teacherService.updateById(eduTeacher);
        if (b) {
            return R.ok();
        }
        return R.error();
    }
}

